package com.lagou.homework.stage01.module04.userMessgaeTest;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {

    public static void main(String[] args) {
        Socket s = null;
        Scanner sc = new Scanner(System.in);
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;

        try {
            s=  new Socket("127.0.0.1",8888);
            System.out.println("Please input user name and password :");

            String userName = sc.next();
            String userPswd = sc.next();
            User user = new User(userName,userPswd);

            oos = new ObjectOutputStream(s.getOutputStream());
            oos.writeObject(user);
            System.out.println("Send User SUCCESS !");

            ois = new ObjectInputStream(s.getInputStream());
            UserMessage um =(UserMessage) ois.readObject();

            System.out.println("Login "+um.getType()+" !");

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(null!=oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null!=ois){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null!=s){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
